package com.gklyj.controller.zhaoping;

import com.gklyj.pojo.Position;
import com.gklyj.pojo.Zixun;
import com.gklyj.service.userService;
import com.gklyj.service.zixunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@Transactional
public class GonggaoController {

    @Autowired
    zixunService zixunService;

    @Autowired
    userService userService;

    /*
    跳转资讯管理页面
     */
    @GetMapping("/toZpGonggaoIndex")
    public String toZpGonggaoIndexPage(){
        return "zhaoping/zp-gonggao";
    }

    /*

     */
    @GetMapping("/ListThisZpGonggao")
    @ResponseBody
    public List<Zixun> ListThisZpGonggao(HttpServletRequest request){
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        return zixunService.ListThisGonggao(department);
    }

    /*
    招聘单位根据条件查询相应的资讯信息
     */
    @PostMapping("/thisZpGonggaoSearch")
    @ResponseBody
    public List<Zixun> thisZpGonggaoSearch(HttpServletRequest request,@RequestBody Zixun zixun){
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(username);
        zixun.setDepartment(department);
        return zixunService.ThisZpGonggaoSearchs(zixun);
    }

    /*
    跳转编辑页面
     */
    @GetMapping("/toZpGonggaoEditPage")
    public String toZpGonggaoEditPage(@RequestParam("id") String id, Model model){
        Zixun zixun = zixunService.getById(Integer.valueOf(id));
        model.addAttribute("Zixun",zixun);
        return "zhaoping/zp-gonggao-edit";
    }

    /*
    跳转管理员资讯添加页面
     */
    @GetMapping("/toZpGonggaoAdd")
    public String toZpGonggaoAddPage(){
        return "zhaoping/zp-gonggao-add";
    }

    /*
    招聘单位添加资讯信息
     */
    @PostMapping("/AddZpGonggao")
    @ResponseBody
    public boolean AddZpGonggao(HttpServletRequest request,@RequestBody Zixun zixun) throws RuntimeException{
        //获取当前登录的招聘单位信息
        String username= (String) request.getSession().getAttribute("username");
        String name = userService.findNameByUsername(username);
        String department = userService.findDepartmentByUsername(username);
        zixun.setU_name(name);
        zixun.setDepartment(department);
        //添加资讯创建时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String str= df.format(now);
        zixun.setTime(str);
        return zixunService.save(zixun);
    }

}
